//
//  NewsTableViewController.m
//  CarHome
//
//  Created by lanou3g on 15-5-2.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "NewsTableViewController.h"
#import "News.h"
#import "NewsInfo.h"
#import "NewsTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface NewsTableViewController ()

@property (nonatomic, retain) News *news;
@property (nonatomic, retain) NewsInfo *newsInfo;
@property (nonatomic, retain) NSMutableArray *list;

@end

@implementation NewsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"NewsTableViewCellIdentifier"];
     [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadData)];
     [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.list = [NSMutableArray array];
    
//    [self loadData];
   
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [self.tableView reloadData];
}



- (void)loadData {
    
    if (_list.count == 0) {
        [self.tableView addLegendHeaderWithRefreshingBlock:^{
            NSLog(@"%@", NewsURLwithCarID(_seriesID));
            __block typeof(self) weakSelf = self;
            
            [HTTPRequest requestWithURL:NewsURLwithCarID(_seriesID) method:@"GET" result:^(NSData *data) {
                NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                self.news = [[News new] autorelease];
                [_news setValuesForKeysWithDictionary:dict[@"result"]];
                
                self.list = [[NSMutableArray alloc]initWithArray:_news.list];
                
                [weakSelf.tableView reloadData];
                [self.tableView.header endRefreshing];
            }];
        }];
        [self.tableView.header beginRefreshing];
    } else {
        [self.tableView reloadData];
    }
    
}


- (void)loadMoreData {
    self.list = [NSMutableArray array];
    
    NSLog(@"%@", NewsURLwithCarID(_seriesID));
    __block typeof(self) weakSelf = self;
    
    [HTTPRequest requestWithURL:NewsURLwithCarID(_seriesID) method:@"GET" result:^(NSData *data) {
        NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.news = [[News new] autorelease];
        [_news setValuesForKeysWithDictionary:dict[@"result"]];
        
        self.list = [[NSMutableArray alloc]initWithArray:_news.list];
        
        [weakSelf.tableView reloadData];
        [self.tableView.header endRefreshing];
    }];
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCellIdentifier" forIndexPath:indexPath];
    
    NewsInfo *ni = _list[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:ni.smallpic] placeholderImage:nil];
    cell.title.text = ni.title;
    cell.time.text =  ni.updatetime;
    cell.replycount.text = [NSString stringWithFormat:@"%@评论", ni.replycount];
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
